/*
 * Created by JFormDesigner on Thu Aug 15 15:18:47 CST 2024
 */

package Student;

import Admin.Admin;
import Admin.ShowCourse;
import bean.Course;
import bean.SelectCourse;

import java.awt.*;
import java.awt.event.*;
import java.util.List;
import javax.swing.*;

import static GetBound.getbound.getBound;
import static JDBCutil.JDBCUtil.*;


/**
 * @author 29454
 */
public class ForStudent extends JFrame {
    private int studentid;
    public ForStudent(int id) {
        studentid = id;
        initComponents();
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setBounds(getBound());
        setExtendedState(JFrame.MAXIMIZED_BOTH);
    }

    private void ShowAllCourse(ActionEvent e) throws Exception {
        // TODO add your code here
        JPanel panel = (JPanel) scrollPane1.getViewport().getView();
        if (panel != null) {
            panel.removeAll();  // 清空面板所有组件
            panel.revalidate();  // 重新验证面板布局
            panel.repaint();  // 重新绘制面板}
        }

        scrollPane1.getViewport().revalidate();  // 重新验证视口
        scrollPane1.getViewport().repaint();  // 重新绘制视口
        try {
            ShowCourse showc = new ShowCourse();
            showc.setVisible(true);
            scrollPane1.setViewportView(showc);
        }
        catch (Exception ex) {
            throw new RuntimeException(ex);
        }
    }

    private void ShowSelectedCourse(ActionEvent e) throws Exception {
        // TODO add your code here
        List<Course> list=getSelectedCourseList(studentid);
        ShowSelectedCourseForStu s=new ShowSelectedCourseForStu(list);
        scrollPane1.setViewportView(s);
    }

    private void ShowCompletedCourse(ActionEvent e) throws Exception {
        // TODO add your code here
        List<SelectCourse>  list=getCompletedCourseList(studentid);
        ShowCompletedCourse s=new ShowCompletedCourse(list);
        scrollPane1.setViewportView(s);
    }

    private void Select(ActionEvent e) throws Exception {
        // TODO add your code here
        int[] ids = Admin.getSelectedid(ShowCourse.getTable1());
        for (int i = 0; i < ids.length; i++) {
            Course course=getCourse(ids[i]);
            SelectCourse sc=new SelectCourse();
            sc.setCourseid(course.getCourseid());
            sc.setStudentid(studentid);
            sc.setMark(-1);
            sc.setTeacherid(course.getTeacherid());
            addSelectCourse(sc);
        }
    }

    private void Cancel(ActionEvent e) throws Exception {
        // TODO add your code here
        int[] ids = Admin.getSelectedid(ShowSelectedCourseForStu.getTable1());
        String[] num=new String[ids.length];
        for (int i = 0; i < ids.length; i++) {
            Course course=getCourse(ids[i]);
            num[i]=course.getCourseid().toString()+Integer.toString(studentid)+course.getTeacherid().toString();

        }
        deleteSelectCourse(num);
        List<Course> list=getSelectedCourseList(studentid);
        ShowSelectedCourseForStu s=new ShowSelectedCourseForStu(list);
        scrollPane1.setViewportView(s);
    }

    private void initComponents() {
        // JFormDesigner - Component initialization - DO NOT MODIFY  //GEN-BEGIN:initComponents  @formatter:off
        button1 = new JButton();
        button2 = new JButton();
        button3 = new JButton();
        scrollPane1 = new JScrollPane();
//        table1 = new JTable();
        button4 = new JButton();
        button5 = new JButton();

        //======== this ========
        Container contentPane = getContentPane();
        contentPane.setLayout(null);

        //---- button1 ----
        button1.setText("\u6240\u6709\u8bfe\u7a0b");
        button1.addActionListener(e -> {try {
ShowAllCourse(e);} catch (Exception ex) {
    throw new RuntimeException(ex);
}});
        contentPane.add(button1);
        button1.setBounds(new Rectangle(new Point(5, 5), button1.getPreferredSize()));

        //---- button2 ----
        button2.setText("\u5df2\u9009\u8bfe\u7a0b");
        button2.addActionListener(e -> {try {
ShowSelectedCourse(e);} catch (Exception ex) {
    throw new RuntimeException(ex);
}});
        contentPane.add(button2);
        button2.setBounds(new Rectangle(new Point(88, 5), button2.getPreferredSize()));

        //---- button3 ----
        button3.setText("\u5df2\u4fee\u8bfe\u7a0b");
        button3.addActionListener(e -> {try {
ShowCompletedCourse(e);} catch (Exception ex) {
    throw new RuntimeException(ex);
}});
        contentPane.add(button3);
        button3.setBounds(new Rectangle(new Point(171, 5), button3.getPreferredSize()));

        //======== scrollPane1 ========
//        {
//            scrollPane1.setViewportView(table1);
//        }
        contentPane.add(scrollPane1);
        scrollPane1.setBounds(0, 45, 1700, 850);

        //---- button4 ----
        button4.setText("\u9009\u8bfe");
        button4.addActionListener(e -> {try {
Select(e);} catch (Exception ex) {
    throw new RuntimeException(ex);
}});
        contentPane.add(button4);
        button4.setBounds(510, 5, 82, 34);

        //---- button5 ----
        button5.setText("\u9000\u8bfe");
        button5.addActionListener(e -> {try {
Cancel(e);} catch (Exception ex) {
    throw new RuntimeException(ex);
}});
        contentPane.add(button5);
        button5.setBounds(610, 5, 82, 34);

        {
            // compute preferred size
            Dimension preferredSize = new Dimension();
            for(int i = 0; i < contentPane.getComponentCount(); i++) {
                Rectangle bounds = contentPane.getComponent(i).getBounds();
                preferredSize.width = Math.max(bounds.x + bounds.width, preferredSize.width);
                preferredSize.height = Math.max(bounds.y + bounds.height, preferredSize.height);
            }
            Insets insets = contentPane.getInsets();
            preferredSize.width += insets.right;
            preferredSize.height += insets.bottom;
            contentPane.setMinimumSize(preferredSize);
            contentPane.setPreferredSize(preferredSize);
        }
        setSize(705, 420);
        setLocationRelativeTo(getOwner());
        // JFormDesigner - End of component initialization  //GEN-END:initComponents  @formatter:on
    }

    // JFormDesigner - Variables declaration - DO NOT MODIFY  //GEN-BEGIN:variables  @formatter:off
    private JButton button1;
    private JButton button2;
    private JButton button3;
    private JScrollPane scrollPane1;
//    private JTable table1;
    private JButton button4;
    private JButton button5;
    // JFormDesigner - End of variables declaration  //GEN-END:variables  @formatter:on
}
